package com.dcxuexi.controller;

import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;

/***
 * @Title UploadFileController
 * @Description TOTD
 * @Auter DongChuang
 * @Date 2022/10/20 20:41
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/upload")
public class UploadFileController {

    @Value("${file.upload.path}")
    private String path;

    @PostMapping("/file")
    public String upload(@RequestPart MultipartFile[] files) throws IOException {
        System.out.println("上传文件路径 ： " + path);
        StringBuilder fileNames = new StringBuilder();

        int i = 0;
        for (MultipartFile file : files) {
            String fileName = file.getOriginalFilename();
            String filePath = path + fileName;

            File dest = new File(filePath);
            Files.copy(file.getInputStream(), dest.toPath());
            fileNames.append(fileName);
            if (i < files.length -1){
                fileNames.append("|");
                i ++;
            }

        }


        return fileNames.toString();
    }

}
